package cc.codeshop.contentresolver;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import cc.codeshop.contentresolver.R;

public class MainActivity extends Activity {
	
	private static final String AUTHORITY = "cc.codeshop.provider";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		getContentResolver().registerContentObserver(uri, notifyForDescendents, observer);
	}
	
	public void testInsert(View view) {
		Uri uri = Uri.parse("content://" + AUTHORITY + "/person");
		ContentValues values = new ContentValues();
		values.put("person_name", "Stan Yung");
		values.put("person_sex", "M");
		Uri replyUri = getContentResolver().insert(uri, values);
		newPersonId = ContentUris.parseId(replyUri);
		Log.d("ccc", "inserte : personId = " + newPersonId);
	}
	
	long newPersonId;
	
	public void testUpdate(View view) {
		Uri uri = Uri.parse("content://" + AUTHORITY + "/person/" + newPersonId);
		ContentValues values = new ContentValues();
		values.put("person_name", "Stan Yung");
		values.put("person_sex", "M");
		getContentResolver().update(uri, values, null, null);
		Log.d("ccc", "update : personId = " + newPersonId);
	}
	
	public void testQueryById(View view) {
		Uri uri = Uri.parse("content://" + AUTHORITY + "/person/" + newPersonId);
		String[] projection = {"person_id", "person_name", "person_sex"};
		Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
		Log.d("ccc", "QueryById : personId = " + newPersonId + ", count = " + cursor.getCount());
		if(cursor.moveToFirst()) {
			do{
				int personId = cursor.getInt(cursor.getColumnIndex("person_id"));
				String personName = cursor.getString(cursor.getColumnIndex("person_name"));
				String personSex = cursor.getString(cursor.getColumnIndex("person_sex"));
				Log.d("ccc", String.format("personId = %d, personName = %s, personSex = %s", personId, personName, personSex));
			} while (cursor.moveToNext());
		}
		
	}
	
	public void testQueryList(View view) {
		Uri uri = Uri.parse("content://" + AUTHORITY + "/person/list");
		String[] projection = {"person_id", "person_name", "person_sex"};
		String selection = "person_sex=?";
		String[] selectionArgs = {"M"};
		Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
		Log.d("ccc", "QueryList : count = " + cursor.getCount());
		if(cursor.moveToFirst()) {
			do{
				int personId = cursor.getInt(cursor.getColumnIndex("person_id"));
				String personName = cursor.getString(cursor.getColumnIndex("person_name"));
				String personSex = cursor.getString(cursor.getColumnIndex("person_sex"));
				Log.d("ccc", String.format("personId = %d, personName = %s, personSex = %s", personId, personName, personSex));
			} while (cursor.moveToNext());
		}
	}
	
	public void testDeleteById(View view) {
		Uri uri = Uri.parse("content://" + AUTHORITY + "/person/" + newPersonId);
		String where = "";
		String[] selectionArgs = {};
		int count = getContentResolver().delete(uri, where, selectionArgs);
		Log.d("ccc", "DeleteById : count = " + count);
	}
	
	public void testDeleteList(View view) {
		Uri uri = Uri.parse("content://" + AUTHORITY + "/person/list");
		String selection = "person_sex=?";
		String[] selectionArgs = {"M"};
		int count = getContentResolver().delete(uri, selection, selectionArgs);
		Log.d("ccc", "DeleteList : count = " + count);
	}
	

}
